package com.todo.strategy.card.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.strategy.card.infrastructure.entity.condition.CardStrategyCondition;
import com.todo.strategy.card.infrastructure.entity.po.CardStrategyPO;
import com.todo.strategy.card.infrastructure.mapper.CardStrategyMapper;
import com.todo.strategy.card.infrastructure.repository.CardStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CardStrategy)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:03
 */
@Slf4j
@Repository("cardStrategyRepository")
public class CardStrategyRepositoryImpl implements CardStrategyRepository {
    @Resource
    private CardStrategyMapper cardStrategyMapper;


    @Override
    public Boolean updateCardStrategyById(CardStrategyPO cardStrategyPO) {
        if (cardStrategyPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(cardStrategyPO)) {
            return false;
        }
        int updateResult = cardStrategyMapper.updateCardStrategyPOById(cardStrategyPO);
        log.info("CardStrategyRepository#updateCardStrategyById 更新数据完成 cardStrategyPO：【{}】", JSONObject.toJSONString(cardStrategyPO));
        return updateResult > 0;
    }

    @Override
    public Boolean insertCardStrategy(CardStrategyPO cardStrategyPO) {
        if (cardStrategyPO == null || cardStrategyPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(cardStrategyPO)) {
            return false;
        }
        int insertResult = cardStrategyMapper.insertCardStrategyPO(cardStrategyPO);
        log.info("CardStrategyRepository#insertCardStrategy 新增数据完成 cardStrategyPO：【{}】", JSONObject.toJSONString(cardStrategyPO));
        return insertResult > 0;
    }

    @Override
    @Transactional
    public Boolean removeCardStrategyPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        int removeResult = cardStrategyMapper.removeCardStrategyPOById(id);
        return removeResult > 0;
    }

    @Override
    public PageResult<CardStrategyPO> queryCardStrategyByCondition(CardStrategyCondition cardStrategyCondition) {
        PageResult<CardStrategyPO> pageResult = new PageResult<>();
        if (cardStrategyCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(cardStrategyCondition.getPageIndex());
        pageResult.setPageSize(cardStrategyCondition.getPageSize());
        long count = queryCountByCondition(cardStrategyCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, cardStrategyCondition.getPageSize()));
        pageResult.setTotal(count);
        List<CardStrategyPO> sightPoiPOList = cardStrategyMapper.queryCardStrategyByCondition(cardStrategyCondition);
        if (CollectionUtils.isEmpty(sightPoiPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(sightPoiPOList);
        pageResult.setCurrentIndex(sightPoiPOList.get(0).getId());
        pageResult.setHasNext(sightPoiPOList.size() >= cardStrategyCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(CardStrategyCondition cardStrategyCondition) {
        if (cardStrategyCondition == null || cardStrategyCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return cardStrategyMapper.queryCountByCondition(cardStrategyCondition);
    }

    @Override
    public CardStrategyPO queryCardStrategyDetailById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        CardStrategyPO cardStrategyPO = cardStrategyMapper.queryCardStrategyPOById(id);
        return cardStrategyPO;
    }

    @Override
    public CardStrategyPO queryCardStrategyById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        CardStrategyPO cardStrategyPO = cardStrategyMapper.queryCardStrategyPOById(id);
        return cardStrategyPO;
    }

    @Override
    public List<CardStrategyPO> queryCardStrategyByCardId(Long cardId) {
        if (cardId == null || cardId < 0) {
            return null;
        }
        return cardStrategyMapper.queryCardStrategyByCardId(cardId);
    }

    private boolean baseFieldCheck(CardStrategyPO cardStrategyPO) {
        if (cardStrategyPO == null) {
            return false;
        }
        if (cardStrategyPO.getCardId() == null || cardStrategyPO.getTitle() == null || cardStrategyPO.getProvince() == null || cardStrategyPO.getCity() == null || cardStrategyPO.getTravelDayType() == null) {
            log.warn("CardStrategyRepository#baseFieldCheck 核心字段信息不存在，不做业务处理 cardStrategyPO：【{}】", JSONObject.toJSONString(cardStrategyPO));
            return false;
        }
        return true;
    }


}
